﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OctoOS.UI;
using OctoOS;
using OctoOS.Services;

public partial class adInfo : OOSPage
{
    class PathInfo
    {
        [PathInfoMember(0)]
        internal long adID;
    }

    PathInfo _pathInfo;
    ClassifiedAd _ad = null;

    protected void Page_Init(object sender, EventArgs e)
    {
#warning admin
        //OOSRequest.Current.EnsureModerator();
        _pathInfo = EnablePathInfo<PathInfo>();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        AddAction("Edit Ad", Urls.Classifieds.GetEditAd(_pathInfo.adID));
        AddAction("Abuse Ad", Urls.Classifieds.GetAdAbuse(_pathInfo.adID));
        AddBreadcrumb(string.Empty, "/");
        AddBreadcrumb("Ads To Approve List", Urls.Classifieds.GetAdsToApproveList(0));

        _ad = DataSource.ClassifiedAds_GetByID(
            OOSRequest.Current.ResourceID,
            0,
            _pathInfo.adID);
        if (_ad != null)
        {
            AddAction("Delete ad", Urls.Classifieds.GetAdsDelete(_pathInfo.adID));
            Author.Text = _ad.UserID == 0 ? "Anonymous" : _ad.UserID.ToString();
            Category.Text = GetAdCategory();
            PageTitle = "Approve Ad";
            Title.Text = _ad.Title;
            AdText.Text = _ad.AdText;
            Latitude.Text = _ad.Latitude == 0 ? "not defined" : _ad.Latitude.ToString();
            Longitude.Text = _ad.Longitude == 0 ? "not defined" :_ad.Longitude.ToString();
            PostTime.Text = _ad.PostTime.ToShortDateString();
            ExpiryTime.Text = _ad.ExpiryTime.ToShortDateString();
            PriceType.Text = _ad.PriceType == 0 ? "Free" :
                _ad.PriceType == 0 ? "Please contact" : "Money";
            Price.Text = _ad.Price.ToString("F2");
            Currency.Text = _ad.CurrencyCode;
            AdType.Text = _ad.Searching == false ? "Offer" : "Need";
            Phone.Text = _ad.ContactPhone;
            Mail.Text = _ad.ContactEMail;
            Url.Text = _ad.ContactUrl;
            foreach (var attribute in _ad.Attributes.OrderByDescending(f => f.SortOrder))
            {
                // if there is choosen value
                if (!attribute.IsCustom)
                {
                    if (attribute.AttributeValues.Where(f => f.Selected == true).Count() > 0)
                    {
                        FormRow row = new FormRow();
                        row.Label = attribute.Title;
                        var value = attribute.AttributeValues.FirstOrDefault(f => f.Selected == true);
                        if (value != null)
                        {
                            OctoOS.UI.Literal lit = new OctoOS.UI.Literal(value.AttributeValueName);
                            row.Controls.Add(lit);
                            FormView.Controls.AddAt(FormView.Controls.IndexOf(FormView.FindControl("LongitudeRow")) + 1, row);
                        }
                        else
                        {
                            break;
                        }

                    }
                }
                else
                {
                    if (attribute.AttributeValues.Count() > 0)
                    {
                        FormRow row = new FormRow();
                        row.Label = attribute.Title;
                        OctoOS.UI.Literal lit = new OctoOS.UI.Literal(attribute.AttributeValues[0].AttributeValueName);
                        row.Controls.Add(lit);
                        FormView.Controls.AddAt(FormView.Controls.IndexOf(FormView.FindControl("LongitudeRow")) + 1, row);
                    }
                }
            }
            AddEmptyRow();
            if (_ad.UserID != 0) AddTextArea();
            AddSubmitButtons();
        }
        else
        {
            throw new HttpException(404, "This page is not found");
        }
    }

    private void AddEmptyRow()
    {
        FormRow row = new FormRow();
        row.VerticalLayout = true;
        var br = new System.Web.UI.WebControls.Literal();
        br.Text = "<br/>";
        var hr = new System.Web.UI.WebControls.Literal();
        br.Text = "<hr/>";
        row.Controls.Add(br);
        row.Controls.Add(hr);
        FormView.Controls.Add(row);
    }

    private void AddTextArea()
    {
        FormRow row = new FormRow();
        row.ID = "MessageRow";
        row.VerticalLayout = true;
        row.Label = "Message to ad's author:";
        TextArea area = new TextArea();
        area.ID = "MessageText";
        area.MaxLength = 300;
        row.Controls.Add(area);
        FormView.Controls.Add(row);
    }

    private void AddSubmitButtons()
    {
        FormRow row = new FormRow();
        row.ID = "SubmitRow";
        Submit approve = new Submit();
        approve.ID = "Approve";
        approve.Text = "Approve Ad";
        approve.Click += new EventHandler(ApproveClick);

        Submit decline = new Submit();
        decline.ID = "Decline";
        decline.Text = "Decline Ad";
        decline.Click+=new EventHandler(DeclineClick);
        row.Controls.Add(approve);
        row.Controls.Add(decline);
        FormView.Controls.Add(row);
    }

    private string GetAdCategory()
    {
        string result = "";
        Helper.Categories.UpdateCategoryCache();
        Category currentCategory = Helper.Categories.GetCategory(_ad.CategoryID);
        Category parentCategory = Helper.Categories.GetParentCategory(_ad.CategoryID);
        Category grandParentCategory = Helper.Categories.GetGrandParentCategory(_ad.CategoryID);
        if (grandParentCategory != null)
        {
            result = grandParentCategory.Name+" - ";
        }
        if (parentCategory != null)
        {
            result += parentCategory.Name + " - ";
        }
        if (currentCategory != null)
        {
            result += currentCategory.Name;
        }
        return result;
    }

    protected void ApproveClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DataSource.ClassifiedAds_SetStatus(
                _pathInfo.adID,
                (byte)Helper.AdStatus.Active,
                string.Empty);
            Response.Redirect(Urls.Classifieds.GetAdsToApproveList(0));
        }
    }

    private string GetMessageToAuthor()
    {
        string result = string.Empty;
        foreach (var control in FormView.Controls)
        {
            if ((control is FormRow) && (control as FormRow).ID != null && (control as FormRow).ID == "MessageRow")
            {
                foreach (var textArea in (control as FormRow).Controls)
                {
                    return (textArea as TextArea).TrimmedText;
                }
            }
        }
        return result;
    }

    protected void DeclineClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DataSource.ClassifiedAds_SetStatus(
                _pathInfo.adID,
                (byte)Helper.AdStatus.Declined,
                _ad.UserID == 0 ? "" : GetMessageToAuthor());
            Response.Redirect(Urls.Classifieds.GetAdsToApproveList(0));
        }
    }
}